package com.dkm.ec.activity.repository.impl;

import com.dkm.base.core.helper.SpringHelper;
import com.dkm.base.db.persistence.dao.IQueryDao;
import com.dkm.biz.ou.repository.ShopStatRepository;
import com.dkm.biz.repository.AbstractRepository;
import com.dkm.ec.activity.domain.SecKillActivityPartin;
import com.dkm.ec.activity.persistence.dao.SecKillActivityPartinQueryDao;
import com.dkm.ec.activity.persistence.entity.SecKillActivityPartinPo;
import com.dkm.ec.activity.repository.SecKillActivityPartinRepository;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by fanjinyuan on 2017/8/12.
 */
@Repository
public class SecKillActivityPartinRepositoryImpl extends AbstractRepository<String, SecKillActivityPartinPo, SecKillActivityPartin> implements SecKillActivityPartinRepository {

    @Resource
    private SecKillActivityPartinQueryDao secKillActivityPartinQueryDao;
    @Resource
    private ShopStatRepository shopStatRepository;

    @Override
    public SecKillActivityPartin newInstance() {
        SecKillActivityPartin domain = SpringHelper.getBean(SecKillActivityPartin.class);
        domain.setData(new SecKillActivityPartinPo());
        return domain;
    }

    @Override
    public SecKillActivityPartin newInstance(SecKillActivityPartinPo po) {
        SecKillActivityPartin domain = SpringHelper.getBean(SecKillActivityPartin.class);
        domain.setData(po);
        return domain;
    }

    @Override
    protected IQueryDao<String, SecKillActivityPartinPo> getQueryDao() {
        return secKillActivityPartinQueryDao;
    }

    @Override
    public List<SecKillActivityPartinPo> findActivityGoods(String configId, int pageIndex, int pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", configId);
        int startRowIndex = (pageIndex -1) * pageSize;
        int endRowIndex = pageIndex * pageSize;
        params.put("startRowIndex", startRowIndex);
        params.put("endRowIndex", endRowIndex);
        return secKillActivityPartinQueryDao.findByKey("findActivityGoods", params);
    }

    @Override
    public Integer countForShopId(String activityId, String shopId) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        params.put("shopId", shopId);
        return secKillActivityPartinQueryDao.countByKey("countForShopId", params);
    }

    @Override
    public Integer getTotalPage(String activityId, int pageSize) {
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("activityId", activityId);
        Integer totalRow = secKillActivityPartinQueryDao.countByKey("countByActivityId", params);
        int totalPage = (totalRow%pageSize == 0 ? totalRow%pageSize : totalRow%pageSize+1);
        return totalPage;
    }


}
